package com.hilaver.dzmis.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecification;
import com.hilaver.dzmis.basicinfo.BiYarn;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtOrderType;
import com.hilaver.dzmis.codetable.CtProductIdentificationStyle;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtYarnMaterial;
import com.hilaver.dzmis.codetable.CtYarnMeasure;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.order.OrderProductItem;
import com.hilaver.dzmis.product.ProductAccessoryButton;
import com.hilaver.dzmis.product.ProductAccessoryOthers;
import com.hilaver.dzmis.product.ProductAccessoryZip;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.vo.OrderStatistics;
import com.hilaver.dzmis.vo.OrderStatisticsCriteria;

public class OrderProductServiceImpl extends AbstractBaseService {
	
	private CtServiceImpl ctService;
	private ProductIdentificationServiceImpl piService;
	private BiFranceCustomerEshopServiceImpl pceService;

	public OrderProductServiceImpl(){
		this.ctService = new CtServiceImpl();
		this.piService = new ProductIdentificationServiceImpl();
		this.pceService = new BiFranceCustomerEshopServiceImpl();
	}

	@Override
	public String edit(Object obj) throws Exception {
		OrderProduct op = (OrderProduct)obj;
		if(op.getBiFranceCustomerEshop() != null){
			this.pceService.edit(op.getBiFranceCustomerEshop());
		}
		String operation = null;
		if(op.getId() != null){
			operation = "02";
		}else
			operation = "01";
		this.dao.saveOrUpdate(obj);
		super.editSysMbox(obj, operation);
		return "{success: true}";
	}
	
	@Override
	public String delete(int id) throws Exception {
		return super.delete(OrderProduct.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		OrderProduct op = (OrderProduct)this.dao.get(OrderProduct.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(op));
		
		xmlSB.append(this.toXMLFragment(op));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		OrderProduct op = (OrderProduct)this.dao.get(OrderProduct.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), op.getCtSeasonNumber(), this.locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSeasonNumber>");
		
		xmlSB.append("<ctStatureCode>");
		xmlSB.append(op.getProductIdentification().getCtStature());
		xmlSB.append("</ctStatureCode>");
		
		xmlSB.append(this.toXMLFragment(op));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List objList = this.dao.getAllPagination(OrderProduct.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);

			xmlSB.append(this.toXMLFragment((OrderProduct)obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(OrderProduct.class.getName(),
				filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
		
	public String getAll(Integer piId) throws Exception{
		
		return null;
	}
	
	public String toXMLFragment(OrderProduct op) throws Exception {
		this.piService.setLocale(this.locale);
		StringBuffer xmlSB = new StringBuffer();
		
		xmlSB.append("<piId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</piId>");
		
		xmlSB.append("<piReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</piReference>");
		
		xmlSB.append("<piReference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</piReference>");
		
		xmlSB.append("<style>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtCollarType.class.getName(),op.getProductIdentification().getCtCollarType(), locale));
		xmlSB.append(this.ctService.getDescription(CtProductIdentificationStyle.class.getName(),op.getProductIdentification().getCtProductIdentificationStyle(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</style>");

		xmlSB.append("<ctOrderType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtOrderType.class.getName(), op.getCtOrderType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctOrderType>");
		
		xmlSB.append("<reference>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getReference());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</reference>");
		
		xmlSB.append("<ctSeasonNumber>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), op.getProductIdentification().getCtSeasonNumber(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSeasonNumber>");
		
		xmlSB.append("<customerName>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getBiFranceCustomer().getName());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerName>");
		
		xmlSB.append("<customerDepartment>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getBiFranceCustomer().getDepartment());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerDepartment>");
		
		writeFile(realFileDir + op.getProductIdentification().getSketch1Name(), op.getProductIdentification().getSketch1());
		xmlSB.append("<sketch1Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getSketch1Name()==null?"noimage.gif":op.getProductIdentification().getSketch1Name());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch1Name>");
		
		writeFile(realFileDir + op.getProductIdentification().getSketch2Name(), op.getProductIdentification().getSketch2());
		xmlSB.append("<sketch2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getSketch2Name()==null?"noimage.gif":op.getProductIdentification().getSketch2Name());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch2Name>");
		
		writeFile(realFileDir + op.getProductIdentification().getSketch3Name(), op.getProductIdentification().getSketch3());
		xmlSB.append("<sketch3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getProductIdentification().getSketch3Name()==null?"noimage.gif":op.getProductIdentification().getSketch3Name());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch3Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(op));

		xmlSB.append(piService.toXMLFragment(op.getProductIdentification()));
		
		if(op.getBiFranceCustomerEshop() != null){
		xmlSB.append("<biFranceCustomerEshopId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(op.getBiFranceCustomerEshop().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</biFranceCustomerEshopId>");
		}
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(op.getBiFranceCustomerEshop()));
		return xmlSB.toString();
	}
	
	public String doStatistics(OrderStatisticsCriteria osc) throws Exception{
		return listToStr(this.doStatisticsList(osc));
	}
	
	public List<OrderStatistics> doStatisticsList(OrderStatisticsCriteria osc) throws Exception{
		if(osc.getOrderProductId() != null){
			if(osc.getIsYarnNeeded().equals("01")){
				return this.doStatYarnList(new String[]{"orderProduct.id=" + osc.getOrderProductId()}, "orderProduct.id", "desc", osc.getDzColorId(), osc.getYarnId());
			}else{
				return this.doStatTrimmingList(new String[]{"orderProduct.id=" + osc.getOrderProductId()}, "orderProduct.id", "desc");
			}
		}
		if(osc.getPiId() != null){
			if(osc.getIsYarnNeeded().equals("01")){
				return this.doStatYarnList(new String[]{"productDZColor.productIdentification.id="+osc.getPiId()}, "productDZColor.productIdentification.id", "desc", osc.getDzColorId(), osc.getYarnId());
			}else{
				return this.doStatTrimmingList(new String[]{"productDZColor.productIdentification.id="+osc.getPiId()}, "productDZColor.productIdentification.id", "desc");
			}
		}
		if(osc.getDate() != null || osc.getCtSeasonNumber() != null || osc.getCustomerId() != null){
			List<String> filterList = new ArrayList<String>();
			if(osc.getDate() != null){
				filterList.add("orderProduct.createDate like '%" + StringUtils.phaseStrDate(StringUtils.toString(osc.getDate())) + "%'");
			}
			if(osc.getCtSeasonNumber() != null){
				filterList.add("orderProduct.productIdentification.ctSeasonNumber=" + osc.getCtSeasonNumber());
			}
			if(osc.getCustomerId() != null){
				filterList.add("orderProduct.productIdentification.biFranceCustomer.id=" + osc.getCustomerId());
			}
			
			if(osc.getIsYarnNeeded().equals("01")){
				return this.doStatYarnList((String[])filterList.toArray(new String[0]), "orderProduct.id", "desc", osc.getDzColorId(), osc.getYarnId());
			}else{
				return this.doStatTrimmingList((String[])filterList.toArray(new String[0]), "orderProduct.id", "desc");
			}
		}
		
		return null;
	}
	
	private String doStatTrimming(String[] filters, String order, String dir) throws Exception{
		List<OrderStatistics> osList = this.doStatTrimmingList(filters, order, dir);
		return listToStr(osList);
	}
	
	private List<OrderStatistics> doStatTrimmingList(String[] filters, String order, String dir) throws Exception{
		List<OrderProductItem> opiList = this.dao.getAll(OrderProductItem.class.getName(), order, dir, filters);
		
		List<OrderStatistics> osList = new ArrayList<OrderStatistics>();
		for(OrderProductItem opi : opiList){
			ProductIdentification pi = opi.getOrderProduct().getProductIdentification();
			BiMeasureSpecification biMS = pi.getBiMeasureSpecification();
			String[] sizeNames = BiMeasureSpecificationServiceImpl.getSizeName(biMS.getCtStature());
			
			Set<ProductAccessoryButton> btns = pi.getProductAccessoryButtons();
			for (ProductAccessoryButton pab : btns) {
				if(pab.getVariant().equals(opi.getVariant()) || "all".equals(pab.getVariant())){
					OrderStatistics os = getOrderStatistics(osList, pab.getBiAccessoryButton().getReference(), opi.getVariant());
					os.setCtSeasonNumber(this.ctService.getDescription(CtSeasonNumber.class.getName(), pi.getCtSeasonNumber(), this.locale));
					os.setOrderNumber(opi.getOrderProduct().getOrderNumber());
					os.setCustomerName(pi.getBiFranceCustomer().getName());
					os.setCustomerDepartment(pi.getBiFranceCustomer().getDepartment());
					os.setPiReference(pi.getReference());
					os.setTrimmingCustomerReference(pab.getBiAccessoryButton().getCustomerReference());
					os.setCustomerColorCode(pab.getBiAccessoryButton().getCustomerColor());
					
					String supplier = ""; //后加需求，根据 辅料类型确定是 factory,custoer还是DZ。hardcode
					if("01".equals(pab.getBiAccessoryButton().getCtSupplyType())){
						supplier = pab.getBiAccessoryButton().getBiFranceCustomer().getName();
					}else if("02".equals(pab.getBiAccessoryButton().getCtSupplyType())){
						if(Constants.LOCALE_TYPE_EN.equals(locale)){
							supplier = pab.getBiAccessoryButton().getBiFactory().getNameEN();
						}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
							supplier = pab.getBiAccessoryButton().getBiFactory().getNameCN();
						}else{
							supplier = pab.getBiAccessoryButton().getBiFactory().getNameEN();
						}
						
					}else{
						supplier = "DZ";
					}
					os.setCtSupplyType(supplier);
					
															
					Integer abTotal = opi.getSizeTotal(this.getSizeIndex(sizeNames, pab.getForSize()));
					if(abTotal > 0){
						os.setTrimmingNeeded(os.getTrimmingNeeded() + (pab.getUnitNumber() + pab.getExtra()) * abTotal);
						osList.add(os);
					}
				}
			}
			
			Set<ProductAccessoryZip> zips = pi.getProductAccessoryZips();
			for (ProductAccessoryZip paz : zips) {
				if(paz.getVariant().equals(opi.getVariant()) || "all".equals(paz.getVariant())){
					OrderStatistics os = getOrderStatistics(osList, paz.getBiAccessoryZip().getReference(), opi.getVariant());
					os.setCtSeasonNumber(this.ctService.getDescription(CtSeasonNumber.class.getName(), pi.getCtSeasonNumber(), this.locale));
					os.setOrderNumber(opi.getOrderProduct().getOrderNumber());
					os.setCustomerName(pi.getBiFranceCustomer().getName());
					os.setCustomerDepartment(pi.getBiFranceCustomer().getDepartment());
					os.setPiReference(pi.getReference());
					os.setTrimmingCustomerReference(paz.getBiAccessoryZip().getCustomerReference());
					os.setCustomerColorCode(paz.getBiAccessoryZip().getCustomerColor());
					
					String supplier = ""; //后加需求，根据 辅料类型确定是 factory,custoer还是DZ。hardcode
					if("01".equals(paz.getBiAccessoryZip().getCtSupplyType())){
						supplier = paz.getBiAccessoryZip().getBiFranceCustomer().getName();
					}else if("02".equals(paz.getBiAccessoryZip().getCtSupplyType())){
						if(Constants.LOCALE_TYPE_EN.equals(locale)){
							supplier = paz.getBiAccessoryZip().getBiFactory().getNameEN();
						}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
							supplier = paz.getBiAccessoryZip().getBiFactory().getNameCN();
						}else{
							supplier = paz.getBiAccessoryZip().getBiFactory().getNameEN();
						}
						
					}else{
						supplier = "DZ";
					}
					os.setCtSupplyType(supplier);
					
															
					Integer abTotal = opi.getSizeTotal(this.getSizeIndex(sizeNames, paz.getForSize()));
					if(abTotal > 0){
						os.setTrimmingNeeded(os.getTrimmingNeeded() + paz.getUnitNumber() * abTotal);
						osList.add(os);
					}
				}
			}
			
			Set<ProductAccessoryOthers> others = pi.getProductAccessoryOtherses();
			for (ProductAccessoryOthers pao : others) {
				if(pao.getVariant().equals(opi.getVariant()) || "all".equals(pao.getVariant())){
					OrderStatistics os = getOrderStatistics(osList, pao.getBiAccessoryOthers().getAccessoryReference() == null?pao.getBiAccessoryOthers().getApplicationReference():pao.getBiAccessoryOthers().getAccessoryReference(), opi.getVariant());
					os.setCtSeasonNumber(this.ctService.getDescription(CtSeasonNumber.class.getName(), pi.getCtSeasonNumber(), this.locale));
					os.setOrderNumber(opi.getOrderProduct().getOrderNumber());
					os.setCustomerName(pi.getBiFranceCustomer().getName());
					os.setCustomerDepartment(pi.getBiFranceCustomer().getDepartment());
					os.setPiReference(pi.getReference());
					os.setTrimmingCustomerReference(pao.getBiAccessoryOthers().getCustomerReference());
					os.setCustomerColorCode(pao.getBiAccessoryOthers().getCustomerColor());
					
					String supplier = ""; //后加需求，根据 辅料类型确定是 factory,custoer还是DZ。hardcode
					if("01".equals(pao.getBiAccessoryOthers().getCtSupplyType())){
						supplier = pao.getBiAccessoryOthers().getBiFranceCustomer().getName();
					}else if("02".equals(pao.getBiAccessoryOthers().getCtSupplyType())){
						if(Constants.LOCALE_TYPE_EN.equals(locale)){
							supplier = pao.getBiAccessoryOthers().getBiFactory().getNameEN();
						}else if(Constants.LOCALE_TYPE_ZH.equals(locale)){
							supplier = pao.getBiAccessoryOthers().getBiFactory().getNameCN();
						}else{
							supplier = pao.getBiAccessoryOthers().getBiFactory().getNameEN();
						}
						
					}else{
						supplier = "DZ";
					}
					os.setCtSupplyType(supplier);
					
															
					Integer abTotal = opi.getSizeTotal(this.getSizeIndex(sizeNames, pao.getForSize()));
					if(abTotal > 0){
						os.setTrimmingNeeded(os.getTrimmingNeeded() + pao.getUnitNumber() * abTotal);
						osList.add(os);
					}
				}
			}
			
		}
		
		Collections.sort(osList, new Comparator<OrderStatistics>(){
			@Override
			public int compare(OrderStatistics o1, OrderStatistics o2) {
				if(o1.getTrimmingDZReference().compareTo(o2.getTrimmingDZReference()) != 0)
					return o1.getTrimmingDZReference().compareTo(o2.getTrimmingDZReference());
				if(o1.getCustomerName().compareTo(o2.getCustomerName()) != 0)
					return o1.getCustomerName().compareTo(o2.getCustomerName());
				if(o1.getCustomerDepartment().compareTo(o2.getCustomerDepartment()) != 0)
					return o1.getCustomerDepartment().compareTo(o2.getCustomerDepartment());
				if(o1.getPiReference().compareTo(o2.getPiReference()) != 0)
					return o1.getPiReference().compareTo(o2.getPiReference());
				if(o1.getOrderNumber().compareTo(o2.getOrderNumber()) != 0)
					return o1.getOrderNumber().compareTo(o2.getOrderNumber());
				return o1.getVariant().compareTo(o2.getVariant());
				
			}
			
		});
		return osList;
	}
	
	private OrderStatistics getOrderStatistics(List<OrderStatistics> osList, String dzReference, String variant){
		for (OrderStatistics orderStatistics : osList) {
			if(orderStatistics.getTrimmingDZReference().equals(dzReference) && orderStatistics.getVariant().equals(variant)){
				return orderStatistics;
			}
		}
		OrderStatistics os = new OrderStatistics();
		os.setTrimmingDZReference(dzReference);
		os.setVariant(variant);
		os.setTrimmingNeeded(0);
		return os;
	}
	
	private Integer getSizeIndex(String[] sizeNames, String forSize){
		Integer sizeIndex = 0;
		for (int i = 0; i < sizeNames.length; i++) {
			if(sizeNames[i].equals(forSize)){
				sizeIndex = i + 1;
			}
		}
		return sizeIndex;
	}
	
	private String doStatYarn(String[] filters, String order, String dir, Integer dzColorId, Integer yarnId) throws Exception{
		List<OrderStatistics> osList = this.doStatYarnList(filters, order, dir, dzColorId, yarnId);
		return listToStr(osList);
	}
	
	private List<OrderStatistics> doStatYarnList(String[] filters, String order, String dir, Integer dzColorId, Integer yarnId) throws Exception{
		List<OrderProductItem> opiList = this.dao.getAll(OrderProductItem.class.getName(), order, dir, filters);
		
		List<OrderStatistics> osList = new ArrayList<OrderStatistics>();
		for(OrderProductItem opi : opiList){
			ProductIdentification pi = opi.getOrderProduct().getProductIdentification();
			Set<ProductDZColor> pDZColorSet = pi.getProductDZColors();
			for(ProductDZColor pDZColor : pDZColorSet){
				if(opi.getProductDZColor().getVariant().equals(pDZColor.getVariant())){
					if(dzColorId != null && !pDZColor.getBiDZColor().getId().equals(dzColorId)){
						continue;
					}
					if(yarnId != null && !pDZColor.getBiDZColor().getBiYarn().getId().equals(yarnId)){
						continue;
					}
					OrderStatistics os = new OrderStatistics();
					os.setCtSeasonNumber(this.ctService.getDescription(CtSeasonNumber.class.getName(), pi.getCtSeasonNumber(), this.locale));
					os.setOrderNumber(opi.getOrderProduct().getOrderNumber());
					os.setVariant(opi.getProductDZColor().getVariant());
					os.setCustomerName(pi.getBiFranceCustomer().getName());
					os.setCustomerDepartment(pi.getBiFranceCustomer().getDepartment());
					os.setDzColorCode(pDZColor.getBiDZColor().getDzColorCode());
					os.setCustomerColorCode(pDZColor.getBiDZColor().getCustomerColorCode());
					os.setPiReference(pi.getReference());
					BiYarn yarn = pDZColor.getBiDZColor().getBiYarn();
					os.setYarnReference(yarn.getReference());
					os.setYarnAccount(this.ctService.getDescription(CtYarnMeasure.class.getName(), yarn.getCtYarnMeasure(), this.locale) + yarn.getYarnAccount());
					os.setYarnNeeded(pDZColor.getActualWeight().doubleValue() * opi.getTotal());
					
					osList.add(os);
				}
			}
		}
		
		Collections.sort(osList, new Comparator<OrderStatistics>(){
			@Override
			public int compare(OrderStatistics o1, OrderStatistics o2) {
				if(o1.getDzColorCode().compareTo(o2.getDzColorCode()) != 0)
					return o1.getDzColorCode().compareTo(o2.getDzColorCode());
				if(o1.getCustomerName().compareTo(o2.getCustomerName()) != 0)
					return o1.getCustomerName().compareTo(o2.getCustomerName());
				if(o1.getCustomerDepartment().compareTo(o2.getCustomerDepartment()) != 0)
					return o1.getCustomerDepartment().compareTo(o2.getCustomerDepartment());
				if(o1.getPiReference().compareTo(o2.getPiReference()) != 0)
					return o1.getPiReference().compareTo(o2.getPiReference());
				if(o1.getOrderNumber().compareTo(o2.getOrderNumber()) != 0)
					return o1.getOrderNumber().compareTo(o2.getOrderNumber());
				if(o1.getVariant().compareTo(o2.getVariant()) != 0)
					return o1.getVariant().compareTo(o2.getVariant());
				
				return o1.getYarnReference().compareTo(o2.getYarnReference());
			}
			
		});
		
		return osList;
	}
	
	private String listToStr(List<OrderStatistics> osList) throws Exception{
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (Object obj : osList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(1);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(osList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public static void main(String[] args) throws Exception{
		OrderProductServiceImpl os = new OrderProductServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}

}
